<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<title>公告管理</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<link rel="stylesheet" type="text/css"
			media="screen, projection, print"
			href="<%=path%>/ext-3.4.0/resources/css/ext-all.css" />
		<script type="text/javascript"
			src="<%=path%>/ext-3.4.0/adapter/ext/ext-base.js"></script>
		<script type="text/javascript" src="<%=path%>/ext-3.4.0/ext-all.js"></script>
		<style type="text/css">
		html,body{
			height:100%;
		}
		</style>
	</head>
	<body>
		<script type="text/javascript">
    Ext.onReady(function(){
    
    	Ext.BLANK_IMAGE_URL = "<%=path%>/ext-3.4.0/resources/images/default/s.gif";
		Ext.QuickTips.init();
		Ext.form.Field.prototype.msgTarget = "side";
		
		function modfiy(){
			if (grid.getSelectionModel().hasSelection()){
				var records=grid.getSelectionModel().getSelections();
				var mycars=new Array();
		  		for(var i=0;i<records.length;i++){
		  			mycars[i]=records[i].data.id;
		  		}
		    	return mycars
			}else{
		 		top.Ext.MessageBox.alert("提示","请选中要操作的记录!");
		 		return false;
			}
		};
		
		function sendCommend(url){
			Ext.MessageBox.wait("服务器处理中，请稍候......","提示");
			Ext.Ajax.request({
				url:url,
				method:'post',
				success:function(response,options){
					var result = Ext.util.JSON.decode(response.responseText);
					if(result.success){
						//执行成功
						store.reload();
					}
					Ext.Msg.alert("提示",result.message);
				},
				failure:function(){
					Ext.Msg.alert("提示","请求发送未成功。");
					return false;
				}
			});
		}
		
		var createOrUpdate = function(id){
			var addTypeFormPanel = new Ext.form.FormPanel({
                   labelSeparator:':',
                   labelWidth:60,
                   border:false,
                   bodyStyle:'padding:0 0 0 0',
                   frame:true,
                   height:'auto',
                   width: 700,
                   items:[
                   		   new Ext.form.Hidden({
                                   name:'id'
                           }),
                           new Ext.form.TextField({
                                   name:'title',
                                   width:600,
                                   fieldLabel:'标题',
                                   allowBlank:false,
                                   emptyText:"请输入公告标题",
                                   blankText:'公告标题不能为空'
                           }),
                           new Ext.form.HtmlEditor({
                                   name:'body',
                                   width:600,
                                   fieldLabel:'类型描述',
                                   xtype:'textarea',
                                   allowBlank:true,
                                   emptyText:"请输入类型描述",
                                   blankText:'描述类型不能为空'
                           })
                   ]
           	})
            var addTypeWindow = new Ext.Window({
              title: '发布公告',
              width: 750,
              height:310,
              layout: 'fit',
              plain:true,
              resizable:false,
              closable:true,
              bodyStyle:'padding:5px;',
              buttonAlign:'center',
              modal:true,
              items: addTypeFormPanel,
              buttons: [{
                  text:"确定",
                  handler:function(){
                  	if(addTypeFormPanel.form.isValid()){
                             addTypeFormPanel.form.submit({
                                     url:"<%=path%>/manager.do?method=TopicCreateOrUpdate",
                                     method:"post",
                                     success:function(form,action){
                                    		store.reload();
                                    		addTypeWindow.destroy();
                                            Ext.MessageBox.alert("提示","操作成功，您的数据已经被系统受理。");
                                     },
                                     failure:function(form,action){
                                     	addTypeWindow.destroy();                    
                                            Ext.MessageBox.alert("提示","操作失败,请检查数据是否填写完整?");
                                     }
                             })
                            }else{
                            	Ext.MessageBox.alert('提示','提交失败,请检查您的信息是否填写完整?');
                            }
                  }
              },{
                  text: '取消',
                  handler:function(){
                      addTypeWindow.destroy();
                  }
              }]
          	});
      		addTypeWindow.show();
      		//如果是编辑操作，则需要加载一些数据。
      		if(id){
      			addTypeFormPanel.getForm().load({
			         waitMsg: '正在加载数据',
			         waitTitle: '提示',
			         url: "manager.do?method=findTopicById&id="+id,
			         method: 'get',
			         success: function(form, action) {
			            //Ext.Msg.alert('提示', '加载成功');
			         },
			         failure:function(form,action){
			         	alert("加载失败");
			         	return false;
			         }
			     });
      		}
		}
		
		/******************************
		 *         定义表格组件         *
		 ******************************/
		
		//定义菜单选择项
		var toolbar = new Ext.Toolbar({
			width:'100%',
			border:false
		});
		toolbar.add(
			new Ext.Button({
				text:'+ 添加公告',
				handler:function(){
					createOrUpdate();
				}
			}),
			'-',
			new Ext.Button({
				text:'- 删除公告',
				handler:function(){
					var ids = modfiy();
					if(ids.toString()){
						var url = "<%=path%>/manager.do?method=TopicsDelete&ids="+ids.toString();
						sendCommend(url);
					}
			 	}
			})
		)
		
		
		var store = new Ext.data.Store({
			reader:new Ext.data.JsonReader({
				root:'items',
				totalProperty:'results',
				fields:[
					{name:'id'},
					{name:"title"},
					{name:"updatetime"}
				]
			}),
			proxy:new Ext.data.HttpProxy({
				url:'<%=path%>/manager.do?method=getTopicsList'
			})
		})
		
		var sm = new Ext.grid.CheckboxSelectionModel();         //定义选择模式
		var grid = new Ext.grid.GridPanel({
			title:"公告管理",
			height:'100%',
			frame:true,
			stripeRows:true,
			tortable:true,
			loadMask: {msg:'正在加载数据，请稍侯...'},
			tbar:[toolbar],
			sm:sm,
			store:store,
			viewConfig: {columnsText:'显示列',sortAscText:'升序',sortDescText:'降序' },
			listeners:{
				dblclick:function(){
					var ids = modfiy();
					if(ids.length == 1){
						//编辑操作
						//Ext.Msg.alert("提示","编辑操作。");
						createOrUpdate(ids.toString());
					}else{
						Ext.Msg.alert("提示","请选择一条记录进行编辑。");
						return false;
					}
				}
			},
			bbar:new Ext.PagingToolbar({
				pageSize:25,  
	            store:store,
	            beforePageText:"当前第",  
	            afterPageText:"页，共{0}页",  
	            lastText:"尾页",  
	            nextText :"下一页",  
	            prevText :"上一页",  
	            firstText :"首页",  
	            refreshText:"刷新页面",  
	            displayInfo: true,  
	            displayMsg:"当前显示 {0} - {1}条, 共 {2}"
			}),
			columns:[
				sm,
				{header:'标题',width:400,dataIdex:'resName',sortabel:true},
				{header:'发布时间',width:200,dataIdex:'eduType',sortabel:true,renderer:function(value,cellmeta,record,rowIndex,columnIndex,color_store){
			            if(value!=null){
			            	return uptime=new Date(value.time).format('Y-m-d');
			            }else{
			            	return null;
			            }
			       	}
       			}]
		});
		
		//主面板
		new Ext.Viewport({
			layout:'border',
			bodyBorder:false,
			border:false,
			items:[
				{
					title:'',
					collapsible:false,
					region:'center',
					layout:'fit',
					items:[grid]
				}
			]
		});
		store.load();
    })
    </script>
	</body>
</html>
